const utils = require("../../../utils/util.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageIndex: 1,
    pageSize: 8,
    total: 0,   //总记录条数  
    hasMoreData: true,
    contentlist: [], 
    shopid:0,
    tipText:'',
    isLoadingMore: false,  //是否正在加载更多数据
    hasData:true
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.setData({
      shopid: options.shopid
    });
    that.getPostInfo(options.shopid);
  },
  getPostInfo: function (shopid) {
    var that = this
    var data = {
      shopId: shopid,
      pageIndex: that.data.pageIndex,
      pageSize: that.data.pageSize
    }
    utils.getData(utils.apiUrl + "/CarCircle/SearchMyPost", data,  function (res) {
      console.log(res)
       if (res.status == 0) {

        var contentlist = res.data.List;
        var total = res.data.Total;
        var hasMore = false;
        var hasData = false;
        if (contentlist.length < total) {
          hasMore = true;
        }
        if(total>0)
        {
          hasData=true;
        }
        var arr=[];
        contentlist.forEach((item, index) => {
          var day = item.ShowTime[0];
          if (item.ShowTime.length > 1) day = item.ShowTime[0] + item.ShowTime[1];
          if (that.contains(arr, day))
          {
            item.showDay = true;
          }else
          {
            arr.push(day);
            item.showDay = false;
          }          
        });
        that.setData({
          contentlist: contentlist,
          hasMoreData: hasMore,
          total: total,
          hasData: hasData
        })
      } else {
        wx.showToast({
          title: '查询失败',
          icon: 'none'
        })
      }
    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
        icon: 'none'
      })

    })
    //wx.hideLoading();
  },
  contains:function (arr,item) {
    for (var i in arr) {
      if (arr[i] == item) return true;
    }
    return false;
  },
  /**
* 页面相关事件处理函数--监听用户下拉动作
*/
  onPullDownRefresh: function () {
    var that = this
    that.data.pageIndex = 1
    that.getPostInfo(that.data.shopid);
    wx.stopPullDownRefresh();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    if (!that.data.hasMoreData) {
      that.setData({
        tipText: "没有更多数据了"
      });
      return;
    }
    else {
      that.setData({
        tipText: "正在加载更多数据.."
      });
      if (that.data.isLoadingMore) {
        return;
      }
      that.setData({
        pageIndex: that.data.pageIndex + 1,
        isLoadingMore: true
      });
      utils.getData(
        utils.apiUrl + '/CarCircle/SearchMyPost',
        {
          shopId: that.data.shopid,
          pageIndex: that.data.pageIndex,
          pageSize: that.data.pageSize
        },
        function (response) {
          if (response.status == 0) {
            var tempList = that.data.contentlist;
            var data = response.data;
            var result = data.List;
            var total = data.Total;
            var listCombine = tempList.concat(result);
            var hasMoreData = false;
            if (listCombine.length < total) {
              hasMoreData = true;
            }
            var arr = [];
            listCombine.forEach((item, index) => {
              var day = item.ShowTime[0];
              if (item.ShowTime.length > 1) day = item.ShowTime[0] + item.ShowTime[1];
              if (that.contains(arr, day)) {
                item.showDay = true;
              } else {
                arr.push(day);
                item.showDay = false;
              }
            });
            that.setData({
              contentlist: listCombine,
              isLoadingMore: false,
              hasMoreData: hasMoreData,
              tipText:''
            });
          }
        },
        function () { },
        function () {
          wx.hideLoading();
        }
      );
    }
   
  },
  showDetail:function(e)
  {
    var postid = e.currentTarget.dataset.postid;
    var posttype = e.currentTarget.dataset.posttype;
    wx.navigateTo({
      url: '../../carcircle/carcircledetails/carcircledetails?postid=' + postid+'&posttype='+posttype
    })
  }
})